package com.taiyi.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taiyi.crm.mapper.CustomerMapper;
import com.taiyi.crm.pojo.entity.Customer;
import com.taiyi.crm.service.ICustomerService;
import com.taiyi.crm.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> selectAll() {
        return customerMapper.selectByPage(new Customer());
    }

    @Override
    public List<Customer> selectCustomerLoss() {
        return customerMapper.selectCustomerLoss();
    }

    @Override
    public LayUITableResult selectByPage(Integer page, Integer limit, Customer customer) {
        //1、使用PageHelper插件设置分页
        PageHelper.startPage(page, limit);
        //2、执行查询
        List<Customer> list = customerMapper.selectByPage(customer);
        //3、使用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        Integer count = (int) pageInfo.getTotal();//得到总数量

        return  LayUITableResult.ok(count, list);
    }

    @Override
    public void insert(Customer customer) {
        customerMapper.insert(customer);
    }

    @Override
    public void deleteById(Integer id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

}